package com.net.xpay.common.manager.impl.stat;

import com.google.common.base.Preconditions;
import com.net.xpay.common.manager.stat.PartnerUserDayStatManager;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractManager;
import com.net.xpay.common.dao.stat.PartnerUserDayStatMapper;
import com.net.xpay.common.domain.stat.PartnerUserDayStat;
import com.net.xpay.common.model.PartnerUserDayStatModel;
import com.net.xpay.common.model.PartnerUserMonthStatModel;
import com.net.xpay.common.model.PartnerUserSumModel;
import com.net.xpay.common.model.search.PartnerUserDayStatSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;


@Component
public class PartnerUserDayStatManagerImpl extends AbstractManager<PartnerUserDayStat> implements PartnerUserDayStatManager {

    @Autowired
    private PartnerUserDayStatMapper mapper;


    @Override
    protected BasicMapper<Long, PartnerUserDayStat> getMapper() {
        return mapper;
    }

    @Override
    public PartnerUserDayStat findByDayAndUserId(LocalDate day, Long userId) {
        PartnerUserDayStat condition = new PartnerUserDayStat();
        condition.setDay(day);
        condition.setUserId(userId);
        return getByCondition(condition);
    }

    @Override
    public List<PartnerUserDayStatModel> aggSearch(PartnerUserDayStatSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("total_trans_amount", "desc");
        page.addOrder("user_id", "desc");
        return mapper.aggSearch(searchModel, page) == null ? new ArrayList<>() : mapper.aggSearch(searchModel, page);
    }

    @Override
    public long aggSearchCount(PartnerUserDayStatSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.aggSearchCount(searchModel);
    }

    @Override
    public PartnerUserMonthStatModel sumPartnerUserDayStatInfo(Long partnerId, LocalDate minDay, LocalDate maxDay) {
        Preconditions.checkArgument(partnerId != null || minDay != null || maxDay !=null);
        return mapper.sumPartnerUserDayStatInfo(partnerId,minDay,maxDay);
    }

    @Override
    public PartnerUserSumModel aggSum(PartnerUserDayStatSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.aggSum(searchModel);
    }
}
